package myaop.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MyAopLogger {
    //前置
    public void before(JoinPoint jp){
        System.out.println("进入了"+ jp.getTarget()+"增强方法(before/前置)" +
                         "   ,方法名是:"+ jp.getSignature().getName()+
                            "参数是: "+ Arrays.toString(jp.getArgs()));
    }

    /**
     * after 最终增强 -可以拿到目标方法的返回值,但是不能重新定义
     * @param jp 目标方法的基础信息如:对象类型,方法名称,参数等等
     * @param obj 执行完方法之后的返回值
     * @return
     */
    public Object myAfter(JoinPoint jp,Object obj){
        System.out.println("进入了"+ jp.getTarget()+"增强方法(after/后置)" +
                "   ,方法名是:"+ jp.getSignature().getName()+
                "参数是: "+ Arrays.toString(jp.getArgs())+",返回值是:"+ obj);

      /*  List<String> list = new ArrayList<>();//List<String>) obj;
        list.add("校草AAA");*/
        //list.remove(0);
        return obj;
    }
    //环绕
    public Object myAround(ProceedingJoinPoint jp){
        System.out.print("进入了"+ jp.getTarget()+"增强方法(around/环绕)" +
                "   ,方法名是:"+ jp.getSignature().getName()+
                "参数是: "+ Arrays.toString(jp.getArgs()));
        try {
            Object result=jp.proceed ();//执行目标方法
            System.out.println(", 修改前返回值是: "+ result);

            List<String> list = new ArrayList<>();//List<String>) obj;
            list.add("校草AAA");
            return  list;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return null;
        }finally {
            System.out.println(">>>>> 环绕增强执行完毕");
        }

    }

    //异常
    public void myAfterThrowing(JoinPoint jp,Exception ex_hhl){
        System.out.println(jp.getTarget()+"."+jp.getSignature().getName()+"方法发生了异常"+ ex_hhl.getMessage());
    }
}
